<?php
if($_SESSION['bbkeluar']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);
?>

<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$("#tgl_keluar").datetimepicker({
					changeMonth: true,
				    changeYear: true,
				    showOtherMonths: true,
					dateFormat:'yy-mm-dd',
					timeFormat: 'hh:mm:ss',
			        hourGrid: 8,
			        minuteGrid: 10,		
				});

			});
</script>

<?php
if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Bahan Baku Keluar</h5>								
						<div class="search global-nav">
							<form method="GET" class="form-search js-search-form" action="index.php?pages=bb_keluar" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="bb_keluar">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="id_prod" <?php echo ($field=="id_prod")?"selected":"";?>>Kode Produksi</option>	
									<option value="kode_bb" <?php echo ($field=="kode_bb")?"selected":"";?>>Kode Bahan Baku</option>
									<option value="status" <?php echo ($field=="status")?"selected":"";?>>Status</option>
									<option value="tgl_keluar" <?php echo ($field=="tgl_keluar")?"selected":"";?>>Tgl. Keluar</option>
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th class="left">Kode Produksi</th>
									<th>Kode B.Baku</th>
									<th>Nama B.Baku</th>
									<th>Jumlah</th>
									<th>Satuan</th>	
									<th>Tgl. Keluar</th>	
									<th>Status</th>		
									<th>#</th>																											
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;

							if($field=='id_prod'){
								$sqlFormula = mysql_query("SELECT id_produksi FROM tb_produksi WHERE kode_prod LIKE '%$q%'");
								$onoFormula = mysql_num_rows($sqlFormula);
								//echo "SELECT id_formula FROM tb_formula WHERE kode LIKE '%$q%'";
								while ($dataFormula = mysql_fetch_array($sqlFormula)) {
									$qx .= $dataFormula['id_produksi'].',';
								}

								if($onoFormula)
									$q2 = substr($qx, 0, -1);
								else
									$q2 = '999999999999999999999';

							}

							if($field=='status'){
								$q2 = (strtolower($q)=='dikirim')?'1':'0';
							}
							
							$addQuery='';


							if(!empty($field)){
								if($field=='id_prod')
									$addQuery .= " WHERE $field IN ($q2) ";
								else
									$addQuery .= " WHERE $field LIKE '%$q2%' ";
							}

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlKode1 = mysql_query("SELECT * FROM tb_bb_keluar $addQuery ORDER BY id_bb_keluar DESC, id_prod limit $posisi, $batas");
							
							$no=$posisi+1;
							while($dataKode = mysql_fetch_array($sqlKode1)){								

								$status = ($dataKode['status']==1)? "<div class=icon-ok title='Dikirim'>&nbsp;</div>":"<div class=icon-remove title='Diproses'>&nbsp;</div>";
								$dataBB = mysql_fetch_array(mysql_query("SELECT nama FROM tb_kode_bb WHERE kode='$dataKode[kode_bb]' LIMIT 1"));
								$dataProduksi = mysql_fetch_array(mysql_query("SELECT kode_prod FROM tb_produksi WHERE id_produksi='$dataKode[id_prod]' LIMIT 1"));
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
									<td><?php echo $dataProduksi['kode_prod'];?></td>
									<td><?php echo $dataKode['kode_bb'];?></td>
									<td><?php echo $dataBB['nama'];?></td>
									<td class="center"><?php echo $dataKode['jumlah'];?></td>
									<td class="center"><?php echo $dataKode['satuan'];?></td>
									<td><?php echo $dataKode['tgl_keluar'];?></td>	
									<td class="center"><?php echo $status;?></td>	
									<td class="action"><a href="?pages=bb_keluar&act=edit&id=<?php echo $dataKode['id_bb_keluar'];?>" class="icon-edit" title="Ubah Data"></a></td>															
								</tr>
							<?php	
								$no++;				
							}

							$sqlSup2 = mysql_query("SELECT * FROM tb_bb_keluar $addQuery ORDER BY id_bb_keluar DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=bb_keluar&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
                                </p><div class="report">
				  <form method="post" action="print_bb_keluar.php" >
					<table width="185" border="1">
  <tr>
    <td width="68">Pilih Bulan</td>
    <td width="101"><select name="bulan" id="bulan">
                                                                <option label="January" value="01" selected="selected">January</option>
                                                                <option label="February" value="02">February</option>
                                                                <option label="March" value="03">March</option>
                                                                <option label="April" value="04">April</option>
                                                                <option label="May" value="05">May</option>
                                                                <option label="June" value="06">June</option>
                                                                <option label="July" value="07">July</option>
                                                                <option label="August" value="08">August</option>
                                                                <option label="September" value="09">September</option>
                                                                <option label="October" value="10">October</option>
                                                                <option label="November" value="11">November</option>
                                                                <option label="December" value="12">December</option>
                                                              </select></td>
    </tr>
  <tr>
    <td colspan="2" align="center"><span class="i"><input type="submit" name="submit" value="Print" /></span></td>
    </tr>
</table>

                                
</form></div></p>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_bb_keluar = (int) filter($_GET['id']);

	$sqlKode = mysql_query("SELECT * FROM tb_bb_keluar WHERE id_bb_keluar=$id_bb_keluar");
	$dataKode = mysql_fetch_array($sqlKode);

	$status = ($dataKode['status']==1)? "checked":"";

	if(!empty($id_bb_keluar)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data Bahan Baku Keluar</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<form method="POST" action="?pages=bb_keluar&act=update">
		<input type="hidden" name="id_bb_keluar" value="<?php echo $dataKode['id_bb_keluar'];?>">	
		<input type="hidden" name="jumlah" value="<?php echo $dataKode['jumlah'];?>">		
		<table style="width:auto">
		   	<tbody>
		   		<tr>
					<td><label>Tgl. Keluar</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataKode['tgl_keluar'];?>" size="30" name="tgl_keluar" id="tgl_keluar" placeholder="Tgl.Keluar" required></td>
			   	</tr>
			   	<tr>
					<td><label>Status</label></td>
					<td>:</td>
					<td><input type="checkbox" name="status" id="status" <?php echo $status;?>></td>
			   	</tr>			   	
			 	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">			 		</td>
		   		</tr>
		 	</tbody>
		</table>
		</form>
	</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_bb_keluar = (int) $_POST['id_bb_keluar'];
	$tgl_keluar = $_POST['tgl_keluar'];
	$status = (isset($_POST['status']))? '1':'0';
	$jumlah = $_POST['jumlah'];
	

	if(!empty($id_bb_keluar) && $status==1){	

		$status2 = mysql_fetch_array(mysql_query("SELECT status2, kode_bb FROM tb_bb_keluar WHERE id_bb_keluar='$id_bb_keluar'"));

		if($status2['status2']!=1){
			$dataStockBB = mysql_fetch_array(mysql_query("SELECT id_kode, stock_bb FROM tb_kode_bb WHERE kode='$status2[kode_bb]' LIMIT 1"));
			$stock_bb = $dataStockBB['stock_bb'];
			$jumlahBB = ($stock_bb - $jumlah);
			$updateBB = mysql_query("UPDATE tb_kode_bb SET stock_bb='$jumlahBB' WHERE id_kode=' $dataStockBB[id_kode]'");
		}

		$update = mysql_query("UPDATE tb_bb_keluar SET tgl_keluar='$tgl_keluar', status='$status', status2='1' WHERE id_bb_keluar='$id_bb_keluar'");
		
		if($update)
			sukses('Data berhasil disimpan', '?pages=bb_keluar');
		else
			sukses('Data gagal disimpan', '?pages=bb_keluar');
			
	}else{
		sukses('Data gagal disimpan', '?pages=bb_keluar');
	}
	
}
?>					